package cn.ffcs.uec.selfstaff.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Messagebox;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.uec.staff.action.bean.UecTitleEditBean;
import cn.ffcs.uec.staff.model.UecStaffInfo;
import cn.ffcs.uec.staff.model.UecTitle;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.PubUtil;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.NodeVo;

@Controller
@Scope("prototype")
public class UecSelfTitleEditComposer extends BasePortletComposer {

	private static final long serialVersionUID = 3086340593438520480L;
	/**
	 * 页面bean
	 */
	private UecTitleEditBean bean = new UecTitleEditBean();
	/**
	 * 操作类型
	 */
	private String opType;
	/**
	 * 人员信息
	 */
	private UecStaffInfo uecStaffInfo;
	/**
	 * 添加教育经历
	 */
	private UecTitle uecTitle;

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		Components.wireVariables(comp, bean);
	}

	/**
	 * 初始化窗口
	 * 
	 * @throws ParseException
	 */
	public void onCreate$uecTitleEditWindow() throws ParseException {
		this.bindCombobox();
		this.bindBean();
	}

	/**
	 * 绑定combobox
	 */
	private void bindCombobox() {
		// 取得资格途径glbdef5
		List<NodeVo> nodeVoGlbdef5List = new ArrayList<NodeVo>();
		String[][] glbdef5 = { { "1001A11000000000067Q", "评审" },
				{ "1001A11000000000067R", "考试" } };
		for (int i = 0; i < glbdef5.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(glbdef5[i][j]);
				nodeVo.setName(glbdef5[i][j + 1]);
			}
			nodeVoGlbdef5List.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getGlbdef5(), nodeVoGlbdef5List);
		// 最高istop
		List<NodeVo> nodeVoIstopList = new ArrayList<NodeVo>();
		String[][] istop = { { "Y", "是" }, { "N", "否" } };
		for (int i = 0; i < istop.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(istop[i][j]);
				nodeVo.setName(istop[i][j + 1]);
			}
			nodeVoIstopList.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getTiptopFlag(), nodeVoIstopList);

		// 职称等级titlerank
		List<NodeVo> nodeVoTitlerankList = new ArrayList<NodeVo>();
		String[][] titlerank = { { "1001A110000000007TV7", "正高级专业技术职务" },
				{ "1001A110000000007TV8", "副高级专业技术职务" },
				{ "1001A110000000007TV9", "中级专业技术职务" },
				{ "1001A110000000007TVA", "助理级专业技术职务" },
				{ "1001A110000000007TVB", "员级专业技术职务" } };
		for (int i = 0; i < titlerank.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(titlerank[i][j]);
				nodeVo.setName(titlerank[i][j + 1]);
			}
			nodeVoTitlerankList.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getTitlerank(), nodeVoTitlerankList);

		// 取得资格途径glbdef5
		List<NodeVo> nodeVoTitleList = new ArrayList<NodeVo>();
		String[][] title = { 
				{"1001A1100000000006OE" , "一级律师"},
				{"1001A1100000000006OF" , "二级律师"},
				{"1001A1100000000006OG" , "三级律师"},
				{"1001A1100000000006OH" , "四级律师"},
				{"1001A1100000000006OI" , "律师助理"},
				{"1001A1100000000006OJ" , "无专业技术职务名称"}
		};
		for (int i = 0; i < title.length; i++) {
			NodeVo nodeVo = new NodeVo();
			for (int j = 0; j < 1; j++) {
				nodeVo.setId(title[i][j]);
				nodeVo.setName(title[i][j + 1]);
			}
			nodeVoTitleList.add(nodeVo);
		}
		ListboxUtils.rendererForEdit(bean.getPkTechposttitle(), nodeVoTitleList);
	}

	/**
	 * bindBean
	 * 
	 * @throws ParseException
	 */
	public void bindBean() throws ParseException {
		opType = (String) arg.get("opType");
		if ("add".equals(opType)) {
			this.bean.getUecTitleEditWindow().setTitle("新增专业技术职务");
			bean.getUecTitleEditDiv().setHeight("30%");
			uecStaffInfo = (UecStaffInfo) arg.get("uecStaffInfo");
		}
		if ("view".equals(opType)) {
			this.bean.getUecTitleEditWindow().setTitle("查看专业技术职务");
			uecTitle = (UecTitle) arg.get("uecTitle");
			if (null != uecTitle) {
				PubUtil.fillBeanFromPo(uecTitle, bean);
				stringTransToDate();
				bean.getButtonDiv().setVisible(false);
				PubUtil.disableComponent(bean.getUecTitleEditWindow(), true);
			}
		}
		if ("edit".equals(opType)) {
			this.bean.getUecTitleEditWindow().setTitle("编辑专业技术职务");
			bean.getUecTitleEditDiv().setHeight("30%");
			uecTitle = (UecTitle) arg.get("uecTitle");
			if (null != uecTitle) {
				PubUtil.fillBeanFromPo(uecTitle, bean);
				stringTransToDate();
			}
		}
	}

	/**
	 * 保存
	 * 
	 * @throws Exception
	 */
	public void onOk() throws Exception {
		String msg = this.checkUacStaffData();
		if (null != msg) {
			Messagebox.show(msg);
			return;
		}
		if ("add".equals(opType)) {
			uecTitle = UecTitle.newInstance();
			PubUtil.fillPoFromBean(bean, uecTitle);
			dateTransToString();
			uecTitle.setStaffId(uecStaffInfo.getStaffId());
			uecTitle.addOnly();
			Events.postEvent("onOK", this.self, uecTitle);
			this.onCancel();
			Messagebox.show("新增成功！");
		}
		if ("edit".equals(opType)) {
			PubUtil.fillPoFromBean(bean, uecTitle);
			dateTransToString();
			uecTitle.updateOnly();
			Events.postEvent("onOK", this.self, uecTitle);
			this.onCancel();
			Messagebox.show("更新成功！");
		}
	}

	/**
	 * 取消
	 */
	public void onCancel() {
		// 关闭窗口
		this.bean.getUecTitleEditWindow().onClose();
	}

	/**
	 * 检测填入的参数是否为空
	 */
	private String checkUacStaffData() {
		// if (StrUtil.isNullOrEmpty(bean.getAbroadway().getValue())) {
		// return "请填写出国出境目的";
		// } else if (StrUtil.isNullOrEmpty(bean.getAbroaddate().getValue())) {
		// return "请填写出国(出境)时间";
		// }else if (StrUtil.isNullOrEmpty(bean.getAbroadarea().getValue())) {
		// return "请填写所去国家";
		// }
		return null;
	}

	/**
	 * 将Date类型时间转成String类型 Date --> String 一套模型，两套标准：日期类型需要格式化
	 */
	public void dateTransToString() {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		if (!StrUtil.isNullOrEmpty(bean.getGlbdef4().getValue())) {
			// 取得资格时间
			uecTitle.setGlbdef4(formatter.format(bean.getGlbdef4().getValue()));
		}
	}

	/**
	 * 将String类型的时间转换成Date类型
	 * 
	 * @throws ParseException
	 */
	public void stringTransToDate() throws ParseException {
		// 时间类型转换：String --> Date
		// 取得资格时间
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		if (null != uecTitle.getGlbdef4()) {
			bean.getGlbdef4().setValue(sdf.parse(uecTitle.getGlbdef4()));
		}
	}

}
